<ui:composition xmlns="http://www.w3.org/1999/xhtml"
	xmlns:ui="http://java.sun.com/jsf/facelets"
	xmlns:h="http://java.sun.com/jsf/html"
	xmlns:f="http://java.sun.com/jsf/core"
	xmlns:p="http://primefaces.org/ui"
	template="../templates/ui.xhtml">
    
        <ui:define name="head">
        <style type="text/css">

        </style>
        
        <script type="text/javascript">
//<![CDATA[

//]]>
        </script>
    </ui:define>

	<ui:define name="content">
		<h1 class="title ui-widget-header ui-corner-all">Tree - ContextMenu</h1>
		<div class="entry">
			<p>Tree has exclusive support for context menu. Different menus can be attached to particular tree nodes by matching nodeType option
            to treeNode type.</p>
			
			<h:form>
			
				<p:growl id="messages" showDetail="true" />
                
                <p:contextMenu for="treeSingle">
                    <p:menuitem value="View" update="messages" actionListener="#{treeBean.displaySelectedSingle}" icon="ui-icon-search" />
                    <p:menuitem value="Delete" update="treeSingle" actionListener="#{treeBean.deleteNode}" icon="ui-icon-close"/>
                </p:contextMenu>

				<p:tree id="treeSingle" value="#{treeBean.root}" var="node"
						selectionMode="single" orientation="horizontal"
						selection="#{treeBean.selectedNode}"
                        dynamic="true">

					<p:treeNode>
						<h:outputText value="#{node}" />
					</p:treeNode>
				</p:tree>
				
			</h:form>
				
		<h3>Source</h3>			
			<p:tabView>
				<p:tab title="treeHorizontalContextMenu.xhtml">
					<pre name="code" class="xml">
&lt;h:form&gt;
			
    &lt;p:growl id="messages" showDetail="true" /&gt;

    &lt;p:contextMenu for="treeSingle"&gt;
        &lt;p:menuitem value="View" update="messages" actionListener="\#{treeBean.displaySelectedSingle}" icon="ui-icon-search" /&gt;
        &lt;p:menuitem value="Delete" update="treeSingle" actionListener="\#{treeBean.deleteNode}" icon="ui-icon-close"/&gt;
    &lt;/p:contextMenu&gt;

    &lt;p:tree id="treeSingle" value="\#{treeBean.root}" var="node"
            selectionMode="single" orientation="horizontal"
            selection="\#{treeBean.selectedNode}"
            dynamic="true"&gt;

        &lt;p:treeNode&gt;
            &lt;h:outputText value="\#{node}" /&gt;
        &lt;/p:treeNode&gt;
    &lt;/p:tree&gt;

&lt;/h:form&gt;
					</pre>
				</p:tab>
				
				<p:tab title="TreeBean.java">
					<pre name="code" class="java">
package org.primefaces.examples.view;

import java.io.Serializable;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;

import org.primefaces.model.DefaultTreeNode;
import org.primefaces.model.TreeNode;

public class TreeBean implements Serializable {

	private TreeNode root;

	private TreeNode selectedNode;

	public TreeBean() {
		root = new DefaultTreeNode("Root", null);
		TreeNode node0 = new DefaultTreeNode("Node 0", root);
		TreeNode node1 = new DefaultTreeNode("Node 1", root);
		TreeNode node2 = new DefaultTreeNode("Node 2", root);

		TreeNode node00 = new DefaultTreeNode("Node 0.0", node0);
		TreeNode node01 = new DefaultTreeNode("Node 0.1", node0);

		TreeNode node10 = new DefaultTreeNode("Node 1.0", node1);
		TreeNode node11 = new DefaultTreeNode("Node 1.1", node1);

		TreeNode node000 = new DefaultTreeNode("Node 0.0.0", node00);
		TreeNode node001 = new DefaultTreeNode("Node 0.0.1", node00);
		TreeNode node010 = new DefaultTreeNode("Node 0.1.0", node01);

		TreeNode node100 = new DefaultTreeNode("Node 1.0.0", node10);
	}

	public TreeNode getRoot() {
		return root;
	}

	public TreeNode getSelectedNode() {
		return selectedNode;
	}

	public void setSelectedNode(TreeNode selectedNode) {
		this.selectedNode = selectedNode;
	}
	
	public void displaySelectedSingle() {
        if(selectedNode != null) {
            FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Selected", selectedNode.getData().toString());

            FacesContext.getCurrentInstance().addMessage(null, message);
        }
	}

    public void deleteNode() {
        selectedNode.getChildren().clear();
        selectedNode.getParent().getChildren().remove(selectedNode);
        selectedNode.setParent(null);
        
        selectedNode = null;
    }
}
					</pre>
				</p:tab>
			</p:tabView>
            </div>
	</ui:define>
</ui:composition>